package com.demo.test;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.UUID;


/**
 * 死信队列消息发送方
 */

@Slf4j
@RequestMapping("/sixin")
@RestController
public class ScheduleController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Value("${deadLetter.normal.exchange}")
    private String topicExchange;

    @GetMapping("/test")
    public void sendEmailMessage() throws UnsupportedEncodingException {

        String msg = RandomStringUtils.randomAlphanumeric(8);
        JSONObject email=new JSONObject();
        email.put("content",msg);
        email.put("to","duchong@qq.com");
        CorrelationData correlationData=new CorrelationData(UUID.randomUUID().toString());
//
//        MessageProperties messageProperties = new MessageProperties();
//        messageProperties.setMessageId(UUID.randomUUID().toString());
//        //30分钟时间太长，这里延时120s消费 设置消息过期 和队列过期时间不一样 两者都有以时间段的过期时间为准
//        messageProperties.setHeader("x-delay", 120000);
//        Message message = new Message(email.toJSONString().getBytes(CharEncoding.UTF_8), messageProperties);
//        rabbitTemplate.convertAndSend(topicExchange,"demo.delay.x",message,correlationData);

        rabbitTemplate.convertAndSend(topicExchange,"deadLetterNormalQueue.x",email.toJSONString(),correlationData);
        log.info("---发送 队列email 消息---{}---messageId---{}",email,correlationData.getId());
    }


}